package com.heima.behavior.service.impl;

import com.baomidou.mybatisplus.extension.api.R;
import com.heima.behavior.service.ApBehaviorEntryService;
import com.heima.behavior.service.ApLikesBehaviorService;
import com.heima.common.exception.CustException;
import com.heima.model.behavior.dtos.LikesBehaviorDTO;
import com.heima.model.behavior.pojos.ApBehaviorEntry;
import com.heima.model.behavior.pojos.ApLikesBehavior;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.threadlocal.AppThreadLocalUtils;
import com.heima.model.user.pojos.ApUser;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
@Slf4j
public class ApLikesBehaviorServiceImpl implements ApLikesBehaviorService {
    @Autowired
    private ApBehaviorEntryService apBehaviorEntryService;
    @Autowired
    MongoTemplate mongoTemplate;

    @Override
    public ResponseResult like(LikesBehaviorDTO dto) {
        ApUser user = AppThreadLocalUtils.getUser();
        Integer userId = user.getId();
        if(userId==0){
            CustException.cust(AppHttpCodeEnum.NEED_LOGIN);
        }
        ApBehaviorEntry apBehaviorEntry = apBehaviorEntryService.findByUserIdOrEquipmentId(userId, dto.getEquipmentId());
//        if (apBehaviorEntry == null) {
//            CustException.cust(AppHttpCodeEnum.NEED_LOGIN);
//        }
        if (dto.getOperation() == 0) {
            saveLikeArticle(apBehaviorEntry, dto);
            return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
        } else if (dto.getOperation() == 1) {
            deleteLikeArticle(apBehaviorEntry, dto);
            return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
        }

        return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
    }

    /**
     * 取消点赞文章
     *
     * @param apBehaviorEntry
     * @param dto
     */
    private void deleteLikeArticle(ApBehaviorEntry apBehaviorEntry, LikesBehaviorDTO dto) {
        Query query = Query.query(Criteria.where("entryId")
                .is(apBehaviorEntry.getId())
                .and("articleId")
                .is(dto.getArticleId()));
        ApLikesBehavior apLikesBehavior = mongoTemplate.findOne(query, ApLikesBehavior.class);
        if (apLikesBehavior != null) {
            mongoTemplate.remove(query,ApLikesBehavior.class);
        }else{
            CustException.cust(AppHttpCodeEnum.NEED_Like);
        }
    }


    /**
     * 点赞文章
     *
     * @param apBehaviorEntry
     * @param dto
     */
    private void saveLikeArticle(ApBehaviorEntry apBehaviorEntry, LikesBehaviorDTO dto) {
        Query query = Query.query(Criteria.where("entryId")
                .is(apBehaviorEntry.getId())
                .and("articleId")
                .is(dto.getArticleId()));
        ApLikesBehavior apLikesBehavior = mongoTemplate.findOne(query, ApLikesBehavior.class);
//        System.err.println("apLikesBehavior = " + apLikesBehavior);
        if (apLikesBehavior == null) {
            ApLikesBehavior apLikesBehavior1 = new ApLikesBehavior();
            Long articleId = dto.getArticleId();
//            System.err.println("articleId = " + articleId);
            apLikesBehavior1.setArticleId(articleId);
            apLikesBehavior1.setEntryId(apBehaviorEntry.getId());
            apLikesBehavior1.setCreatedTime(new Date());
            mongoTemplate.save(apLikesBehavior1);
        }
    }
}
